﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BgManager : MonoBehaviour {

	// 初试数据
	public static BgManager instance;
	public GameObject bg;
	private float bgWide;
	private float bgHight;

	// 当前偏移量
	private float currWide;
	private float currHight;

	//存储背景
	public GameObject leftAndTopBg;
	public GameObject topBg;
	public GameObject rightAndTopBg;
	public GameObject leftBg;
	public GameObject centreBg;
	public GameObject rightBg;
	public GameObject leftAndBottemBg;
	public GameObject bottemBg;
	public GameObject rightAndBottemBg;
	public GameObject tempBg;

	int i = 0;

	/// <summary>
	/// Awake is called when the script instance is being loaded.
	/// </summary>
	void Awake()
	{
		instance = this;

		
	}

	// Use this for initialization
	void Start () {
        // 获得长宽
        bgWide = GameObject.Find("Bg").GetComponent<SpriteRenderer>().bounds.size.x;
        bgHight = GameObject.Find("Bg").GetComponent<SpriteRenderer>().bounds.size.y;

        // 实例化背景
        leftAndTopBg = GameObject.Instantiate(bg, new Vector3(-bgWide, bgHight, 0), Quaternion.identity);
        topBg = GameObject.Instantiate(bg, new Vector3(0, bgHight, 0), Quaternion.identity);
        rightAndTopBg = GameObject.Instantiate(bg, new Vector3(bgWide, bgHight, 0), Quaternion.identity);

        leftBg = GameObject.Instantiate(bg, new Vector3(-bgWide, 0, 0), Quaternion.identity);
        centreBg = GameObject.Find("Bg");
        rightBg = GameObject.Instantiate(bg, new Vector3(bgWide, 0, 0), Quaternion.identity);

        leftAndBottemBg = GameObject.Instantiate(bg, new Vector3(-bgWide, -bgHight, 0), Quaternion.identity);
        bottemBg = GameObject.Instantiate(bg, new Vector3(0, -bgHight, 0), Quaternion.identity);
        rightAndBottemBg = GameObject.Instantiate(bg, new Vector3(bgWide, -bgHight, 0), Quaternion.identity);


    }
	
	// Update is called once per frame
	void Update () {
		
	}

	public void SetBgLocation(Transform heroTransform){
		// print("transform.position.y--->" + heroTransform.position.y);
		// print("topBg.transform.position.y--->" + topBg.transform.position.y);

		currHight = heroTransform.position.y - topBg.transform.position.y;
		//飞机向下
		if(currHight <= -bgHight * 1.5){
			topBg.transform.position = new Vector3(topBg.transform.position.x, topBg.transform.position.y - bgWide * 2, 0);
			leftAndTopBg.transform.position = new Vector3(leftAndTopBg.transform.position.x, leftAndTopBg.transform.position.y - bgWide * 2, 0);
			rightAndTopBg.transform.position = new Vector3(rightAndTopBg.transform.position.x, rightAndTopBg.transform.position.y - bgWide * 2, 0);

			tempBg = topBg;
			topBg = centreBg;
			centreBg = bottemBg;
			bottemBg = tempBg;

			tempBg = leftAndTopBg;
			leftAndTopBg = leftBg;
			leftBg = leftAndBottemBg;
			leftAndBottemBg = tempBg;

			tempBg = rightAndTopBg;
			rightAndTopBg = rightBg;
			rightBg = rightAndBottemBg;
			rightAndBottemBg = tempBg;

		}
		// 飞机向上
		currHight = heroTransform.position.y - bottemBg.transform.position.y;
		if(currHight >= bgHight * 1.5){
			bottemBg.transform.position = new Vector3(bottemBg.transform.position.x, bottemBg.transform.position.y + bgWide * 2, 0);
			leftAndBottemBg.transform.position = new Vector3(leftAndBottemBg.transform.position.x, leftAndBottemBg.transform.position.y + bgWide * 2, 0);
			rightAndBottemBg.transform.position = new Vector3(rightAndBottemBg.transform.position.x, rightAndBottemBg.transform.position.y + bgWide * 2, 0);

			tempBg = topBg;
			topBg = bottemBg;
			bottemBg = centreBg;
			centreBg = tempBg;

			tempBg = leftAndTopBg;
			leftAndTopBg = leftAndBottemBg;
			leftAndBottemBg = leftBg;
			leftBg = tempBg;

			tempBg = rightAndTopBg;
			rightAndTopBg = rightAndBottemBg;
			rightAndBottemBg = rightBg;
			rightBg = tempBg;

		}
		//飞机向左
		currWide = heroTransform.position.x - rightBg.transform.position.x;
		if(currWide <= -bgWide * 1.5){
			rightBg.transform.position = new Vector3(rightBg.transform.position.x - bgWide * 3, rightBg.transform.position.y, 0);
			rightAndTopBg.transform.position = new Vector3(rightAndTopBg.transform.position.x - bgWide * 3, rightAndTopBg.transform.position.y, 0);
			rightAndBottemBg.transform.position = new Vector3(rightAndBottemBg.transform.position.x - bgWide * 3, rightAndBottemBg.transform.position.y, 0);

			tempBg = rightBg;
			rightBg = centreBg;
			centreBg = leftBg;
			leftBg = tempBg;

			tempBg = rightAndTopBg;
			rightAndTopBg = topBg;
			topBg = leftAndTopBg;
			leftAndTopBg = tempBg;

			tempBg = rightAndBottemBg;
			rightAndBottemBg = bottemBg;
			bottemBg = leftAndBottemBg;
			leftAndBottemBg = tempBg;
		}
		// 飞机向右
		currWide = heroTransform.position.x - leftBg.transform.position.x;
		if(currWide >= bgWide * 1.5){
			leftBg.transform.position = new Vector3(leftBg.transform.position.x + bgWide * 3, leftBg.transform.position.y, 0);
			leftAndTopBg.transform.position = new Vector3(leftAndTopBg.transform.position.x + bgWide * 3, leftAndTopBg.transform.position.y, 0);
			leftAndBottemBg.transform.position = new Vector3(leftAndBottemBg.transform.position.x + bgWide * 3, leftAndBottemBg.transform.position.y, 0);

			tempBg = leftBg;
			leftBg = centreBg;
			centreBg = rightBg;
			rightBg = tempBg;

			tempBg = leftAndTopBg;
			leftAndTopBg = topBg;
			topBg = rightAndTopBg;
			rightAndTopBg = tempBg;

			tempBg = leftAndBottemBg;
			leftAndBottemBg = bottemBg;
			bottemBg = rightAndBottemBg;
			rightAndBottemBg = tempBg;
		}
	}
}
